package com.ryskoo.controller.customer;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ryskoo.customer.customer.domain.CustomerPO;
import com.ryskoo.customer.customer.service.ICustomerService;
import com.ryskoo.customer.customeraccount.domain.CustomerAccountPO;
import com.ryskoo.customer.customeraccount.service.ICustomerAccountService;
import com.ryskoo.framework.core.bean.Pager;
import com.ryskoo.framework.core.bean.QueryMessageDTO;
import com.ryskoo.framework.core.bean.ReturnCodeEnum;
import com.ryskoo.framework.core.bean.ReturnMessageDTO;
import com.ryskoo.framework.core.controller.BasicHandler;
import com.ryskoo.framework.core.exception.InvalidParamException;
import com.ryskoo.framework.core.utils.LoginUtil;

/**
 * 会员管理 web controller
 *
 * @author hh
 */
@Controller
@RequestMapping(value = "/vendor/customer/customer")
public class CustomerController extends BasicHandler<CustomerPO> {

	private ICustomerService customerService;

	public CustomerController() {
		super("system/customer/customer", "Customer");
	}

	@Autowired
	public void setService(ICustomerService customerService) {
		this.customerService = customerService;
		super.setService(customerService);
	}
	
	@Autowired
	private ICustomerAccountService customerAccountService;

	@Override
	protected void buildListPageData(Model model, HttpServletRequest request) {
		String vendorId = LoginUtil.getLoginVendorUuid();
		model.addAttribute("vendorId", vendorId);
	}

	
	/**
	 * 查询会员列表
	 * @param response
	 * @param vendorId
	 * @param query
	 * @param page
	 * @param size
	 * @return
	 */
	@PostMapping("/getCustomerByVendorId")
	@ResponseBody
	public ReturnMessageDTO getCustomerByVendorId(@RequestBody QueryMessageDTO qm) {
		List<Map<String, Object>> list = customerService.getCustomerByVendorId(qm);
		int total = customerService.getCustomerCountByVendorId(qm);
        ReturnMessageDTO returnMessage = new ReturnMessageDTO();
        returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
        returnMessage.setReturnMessage(list);
        returnMessage.setPager(new Pager(qm.getPageShow(), qm.getQueryPage(), total));
        return returnMessage;
    }

	@Override
	public String toView(@PathVariable(value = "id") String id, Model model, HttpServletRequest request) {
		CustomerPO customer = customerService.getByUuid(id);
        CustomerAccountPO customerAccount = customerAccountService.getByCustomerUuid(id);
        if (customer != null) {
            customer.setCustomerAccount(customerAccount);
        }
        model.addAttribute("dto",customer);
        return "system/customer/customer/CustomerView";
	}
	
	/**
     * 给会员设置备注
     * @param uuid 会员uuid
     * @param remark 备注
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/setRemart")
    @ResponseBody
    public ReturnMessageDTO<CustomerPO> setRemart(@RequestParam String uuid, @RequestParam String remark) {
    	ReturnMessageDTO<CustomerPO> returnMessage = new ReturnMessageDTO<>();
        try {
            customerService.setRemark(uuid,remark);
            returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
            returnMessage.setReturnNote("设置成功");
        }catch (Exception e){
            returnMessage.setReturnCode(ReturnCodeEnum.ERROR.getReturnCode());
            returnMessage.setReturnNote("设置失败");
        }
        return returnMessage;
    }

}
